package com.robot.mvvm.ui.work

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.blankj.utilcode.util.TimeUtils
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.utils.ChoiceImgHelper
import com.robot.mvvm.BR
import com.robot.mvvm.R
import com.robot.mvvm.databinding.AppActivityAddSafetyBinding
import com.robot.mvvm.vm.work.AddSafetyViewModel
import java.util.*

@Route(path = AppConstants.Router.Main.A_ADD_SAFETY)
class AddSafetyActivity : GLBaseActivity<AppActivityAddSafetyBinding, AddSafetyViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_add_safety

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        addSingleTitleBar(getString(R.string.app_tips_78))
        viewModel.professionalCertTypes()
        viewModel.educations()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.showPhotoEvent.observe(this) {
            it ?: return@observe
            ChoiceImgHelper.choice(
                this,
                1,
                false,
                { it1 ->
                    it1.forEach { it2 ->
                        viewModel.upload(it2?.compressPath ?: "", it) {
                        }
                    }
                },
                {

                }
            )
        }

        viewModel.uc.showDialogEvent.observe(this) {
            when (it) {
                0 -> {
                    initPicker(arrayListOf("男", "女"), it)
                }
                1 -> {
                    val list = viewModel.educationsDicinfoDataDTOBean.map {
                        it.dicname ?: ""
                    }
                    initPicker(list, it)
                }
                2 -> {
                    val list = viewModel.professionalCertTypesDicinfoDataDTOBean.map {
                        it.dicname ?: ""
                    }
                    initPicker(list, it)
                }
                3 -> {
                    initCustomTimePicker()
                }
            }
        }
    }

    private fun initPicker(list: List<String>, type: Int) {
        val pickView = OptionsPickerBuilder(
            this
        ) { options1, options2, options3, v ->
            when (type) {
                0 -> {
                    viewModel.sex.set(list[options1])
                }
                1 -> {
                    viewModel.education.set(list[options1])
                    viewModel.educationCode =
                        viewModel.educationsDicinfoDataDTOBean[options1].parentCode ?: ""
                }
                2->{
                    viewModel.certType.set(list[options1])
                    viewModel.certTypeCode = viewModel.professionalCertTypesDicinfoDataDTOBean[options1].parentCode ?: ""
                }

            }

        }.build<String>()
        pickView.setPicker(list)
        pickView.show()
    }

    var pvCustomTime: TimePickerView? = null
    private fun initCustomTimePicker() {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        val selectedDate = Calendar.getInstance() //系统当前时间
        val startDate = Calendar.getInstance()
        startDate[1900, 0] = 1
//        endDate[2100, 2] = 28
        //时间选择器 ，自定义布局
        pvCustomTime = TimePickerBuilder(
            this
        ) { date, v -> //选中事件回调
            viewModel.gmtCertExpire.set(TimeUtils.date2String(date, "yyyy-MM-dd"))
        }
            .setDate(selectedDate)
            .setContentTextSize(18)
            .setType(booleanArrayOf(true, true, true, false, false, false))
            .setLabel(
                "",
                "",
                "",
                "",
                "",
                ""
            )
            .setLineSpacingMultiplier(3f)
            .setItemVisibleCount(3)
            .isDialog(false)
            .setOutSideCancelable(true)
            .setTextXOffset(0, 0, 0, 40, 0, -40)
//            .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            .build()
        pvCustomTime?.show()
    }
}